﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Toolkit.Web.ExtensionMethods;

namespace Toolkit.Web.ViewComponents.DataGrid
{
    public class DataGridColumnFilterBuilder
    {
        public DataGridColumnFilterBuilder(DataGridColumnFilter component)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }

            Component = component;
        }

        public DataGridColumnFilter Component
        {
            get;
            private set;
        }

        public DataGridColumnFilterBuilder HtmlAttributes(object attributes)
        {
            Component.HtmlAttributes.Merge(attributes, true);
            return this;
        }

        public DataGridColumnFilterBuilder AvailableOperators(params DataGridFilterOperator[] operators)
        {
            if (operators != null)
            {
                return AvailableOperators(operators.ToList());
            }
            return this;
        }

        public DataGridColumnFilterBuilder AvailableOperators(IList<DataGridFilterOperator> operators)
        {
            if (operators != null && operators.Count > 0)
            {
                Component.AvailableOperators = operators;
            }
            return this;
        }

        public DataGridColumnFilterBuilder AvailableValues(params string[] values)
        {
            if (values != null)
            {
                return AvailableValues(values.ToList());
            }
            return this;
        }

        public DataGridColumnFilterBuilder AvailableValues(IList<string> values)
        {
            IDictionary<string, string> options = new Dictionary<string, string>();

            if (values == null)
            {
                return AvailableValues(options);
            }

            foreach (string value in values)
            {
                options.Add(value, value);
            }

            return AvailableValues(options);
        }

        public DataGridColumnFilterBuilder AvailableValues(IDictionary<string, string> options)
        {
            if (Component.Type != DataGridColumnFilterType.SingleSelect && Component.Type != DataGridColumnFilterType.MultiSelect)
            {
                throw new NotSupportedException();
            }

            Component.AvailableValues = options;
            return this;
        }

        public DataGridColumnFilterBuilder AppliedOperator(DataGridFilterOperator filterOperator)
        {
            Component.AppliedOperator = filterOperator;
            return this;
        }

        public DataGridColumnFilterBuilder AppliedValues(IList<string> values)
        {
            Component.AppliedValues = values;
            return this;
        }
    }
}
